/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.channel.web.ChannelAction
   Module Description   :

   Date Created      : 2010/7/5
   Original Author   : feeling
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.channel.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.FastDateFormat;
import org.apache.commons.lang.xwork.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.channel.entity.Channel;
import com.yaodian100.ec.channel.service.ChannelService;
import com.yaodian100.ec.common.EcConstant;

/**
 * @author feeling
 * 
 */
public class ChannelAction extends ActionSupport implements Preparable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6394620845044980582L;
	private final Logger logger = LoggerFactory.getLogger(ChannelAction.class);
	@Autowired
	private ChannelService channelService;
	@Autowired
	private MenuService menuService;

	/* list */
	private Menu channelNameMenu;
	private Menu channelTypeMenu;
	private Menu channelStatusMenu;
	private Map<String, Object> channelStatusMap;

	/* search */
	private String actTypeParam;
	private String actDateParam;
	private String channelNameParam;
	private String channelStatusParam;

	/* edit */
	private String actId;
	private String channelName;
	private String actType;
	private String actName;
	private String actUrl;
	private String startDate;
	private String startTime;
	private String endDate;
	private String endTime;
	private String sTime;
	private String eTime;
	private String channelStatus;

	private Channel channel;
	private SimplePager pager;
	private List<Channel> channelList;

	public String add() throws Exception {
		channel = new Channel();
		channel.setId(null);
		channel.setActType("0");
		channel.setActName(null);
		channel.setActUrl(null);
		channel.setChannelName(null);
		channel.setStartDate(null);
		channel.setEndDate(null);
		channel.setChannelStatus("Y");
		startTime = null;
		endTime = null;
		return "edit";
	}

	public String edit() throws Exception {
		FastDateFormat dateFormat = FastDateFormat.getInstance("HH:mm");
		if (StringUtils.isNotBlank(actId)) {
			channel = channelService.get(Long.parseLong(actId));
			startTime = dateFormat.format(channel.getStartDate());
			endTime = dateFormat.format(channel.getEndDate());
			channelStatus = channel.getChannelStatus();
		}
		return "edit";
	}

	public String getActDateParam() {
		return actDateParam;
	}

	public String getActId() {
		return actId;
	}

	public String getActName() {
		return actName;
	}

	public String getActType() {
		return actType;
	}

	public String getActTypeParam() {
		return actTypeParam;
	}

	public String getActUrl() {
		return actUrl;
	}

	public Channel getChannel() {
		return channel;
	}

	public List<Channel> getChannelList() {
		return channelList;
	}

	public String getChannelName() {
		return channelName;
	}

	public Menu getChannelNameMenu() {
		return channelNameMenu;
	}

	public String getChannelNameParam() {
		return channelNameParam;
	}

	public String getChannelStatus() {
		return channelStatus;
	}

	public Map<String, Object> getChannelStatusMap() {
		return channelStatusMap;
	}

	public Menu getChannelStatusMenu() {
		return channelStatusMenu;
	}

	public String getChannelStatusParam() {
		return channelStatusParam;
	}

	public Menu getChannelTypeMenu() {
		return channelTypeMenu;
	}

	public String getEndDate() {
		return endDate;
	}

	public String getEndTime() {
		return endTime;
	}

	public String geteTime() {
		return eTime;
	}

	public SimplePager getPager() {
		return pager;
	}

	public String getStartDate() {
		return startDate;
	}

	public String getStartTime() {
		return startTime;
	}

	public String getsTime() {
		return sTime;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		clearErrorsAndMessages();
		if (null == pager) {
			pager = new SimplePager();
		}
		pager.setPageRecord(20);// defaut 20

		if (null == channelNameMenu) {
			channelNameMenu = menuService.get("Channel.Name");
		}

		if (null == channelTypeMenu) {
			channelTypeMenu = menuService.get("Channel.Type");
		}

		if (null == channelStatusMenu) {
			channelStatusMenu = menuService.get("Channel.Status");
		}

		channelStatusMap = new LinkedHashMap<String, Object>();
		channelStatusMap.put(channelStatusMenu.getOptions().get("Y").getCode(), channelStatusMenu.getOptions().get("Y")
				.getName());
		channelStatusMap.put(channelStatusMenu.getOptions().get("N").getCode(), channelStatusMenu.getOptions().get("N")
				.getName());

	}

	public String save() throws Exception {
		try {
			DateFormat format = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP);
			Date sDate = null;
			Date eDate = null;
			if (StringUtils.isNotBlank(actId)) {
				channel = channelService.get(Long.parseLong(actId));
			} else {
				channel = new Channel();
			}

			channel.setActName(StringUtils.substring(actName, 0, 30));
			channel.setActType(actType);
			channel.setActUrl(actUrl);
			channel.setChannelName(channelName);
			channel.setChannelStatus(channelStatus);
			try {
				if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(startTime)) {
					sDate = format.parse(startDate + " " + startTime + ":00");
				}
				if (StringUtils.isNotBlank(endDate) && StringUtils.isNotBlank(endTime)) {
					eDate = format.parse(endDate + " " + endTime + ":59");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			channel.setStartDate(sDate);
			channel.setEndDate(eDate);
			channelService.save(channel);
			addActionMessage("资料异动成功");
		} catch (Exception e) {
			addActionError("资料异动失败");
			channel.setId(null);
		}
		return "edit";
	}

	/**
	 * @return
	 * @throws Exception
	 */
	public String search() throws Exception {
		if (StringUtils.isBlank(actTypeParam)) {
			actTypeParam = channelTypeMenu.getOptions().get("0").getCode();
		}
		int channelSize = channelService.getChannelByCondication(channelNameParam, actDateParam, actTypeParam,
				channelStatusParam);
		pager.setTotalSize(channelSize);
		channelList = channelService.getChannelByCondication(channelNameParam, actDateParam, actTypeParam,
				channelStatusParam, pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord());
		return "list";
	}

	public void setActDateParam(String actDateParam) {
		this.actDateParam = actDateParam;
	}

	public void setActId(String actId) {
		this.actId = actId;
	}

	public void setActName(String actName) {
		this.actName = actName;
	}

	public void setActType(String actType) {
		this.actType = actType;
	}

	public void setActTypeParam(String actTypeParam) {
		this.actTypeParam = actTypeParam;
	}

	public void setActUrl(String actUrl) {
		this.actUrl = actUrl;
	}

	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	public void setChannelList(List<Channel> channelList) {
		this.channelList = channelList;
	}

	public void setChannelName(String channelName) {
		this.channelName = channelName;
	}

	public void setChannelNameMenu(Menu channelNameMenu) {
		this.channelNameMenu = channelNameMenu;
	}

	public void setChannelNameParam(String channelNameParam) {
		this.channelNameParam = channelNameParam;
	}

	public void setChannelStatus(String channelStatus) {
		this.channelStatus = channelStatus;
	}

	public void setChannelStatusMap(Map<String, Object> channelStatusMap) {
		this.channelStatusMap = channelStatusMap;
	}

	public void setChannelStatusMenu(Menu channelStatusMenu) {
		this.channelStatusMenu = channelStatusMenu;
	}

	public void setChannelStatusParam(String channelStatusParam) {
		this.channelStatusParam = channelStatusParam;
	}

	public void setChannelTypeMenu(Menu channelTypeMenu) {
		this.channelTypeMenu = channelTypeMenu;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public void seteTime(String eTime) {
		this.eTime = eTime;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public void setsTime(String sTime) {
		this.sTime = sTime;
	}
}
